<template>
  <div class="app-container">
    <el-form
      ref="form"
      class="form"
      :rules="rules"
      :model="form"
      label-width="80px"
    >
      <el-form-item label="鞋架编码" prop="rackCode">
        <el-input-number
          v-model="form.rackCode"
          :min="1"
          step-strictly
          :disabled="form.id"
          placeholder="请输入鞋架编码"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="行" prop="rowNum">
        <el-input-number
          v-model="form.rowNum"
          :min="1"
          :max="5"
          :disabled="form.id"
          step-strictly
          placeholder="请输入行"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="列" prop="colNum">
        <el-input-number
          v-model="form.colNum"
          :min="1"
          :max="8"
          :disabled="form.id"
          step-strictly
          placeholder="请输入列"
        ></el-input-number>
      </el-form-item>

      <el-form-item label="鞋架备注">
        <el-input
          type="textarea"
          v-model="form.remark"
          placeholder="请输入鞋架备注"
        ></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">{{
          form.id ? "保存修改" : "立即创建"
        }}</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { getShoeRackDetail, addOrEditShoeRack } from "@/api/shoeRack/index";

export default {
  data() {
    return {
      form: {
        rackCode: undefined,
        rowNum: "",
        colNum: "",
        remark: "",
      },
      loading: false,
      rules: {
        rackCode: [
          { required: true, message: "请输入鞋架编码", trigger: "blur" },
        ],
        rowNum: [{ required: true, message: "请输入行", trigger: "blur" }],
        colNum: [{ required: true, message: "请输入列", trigger: "blur" }],
      },
    };
  },
  created() {
    if (this.$route.query.id) {
      this.getDetail();
    }
  },
  methods: {
    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          addOrEditShoeRack({
            ...this.form,
            shopId: this.$store.state.user.currentShop.id,
          })
            .then((res) => {
              this.loading = false;
              this.$message.success("操作成功");
              this.$router.push("/shoeRack/list");
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          return false;
        }
      });
    },
    getDetail() {
      getShoeRackDetail(this.$route.query.id).then((res) => {
        this.form = res.data;
      });
    },
  },
};
</script>

<style lang="scss" scoped>
.form {
  width: 480px;
}
</style>
